package com.soup.lab.spring.retry.service.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class RetryableSampleServiceImpl implements SampleService {

    private static final Logger logger = LoggerFactory.getLogger(RetryableSampleServiceImpl.class);
    @Autowired
    RetryTemplate retryTemplate;

    @Override
    public void invoke() {
        retryTemplate.execute(context -> {
            Random random = new Random();
            int mod = random.nextInt(100) % 4;
            logger.info("----invoke----> {}", mod);
            switch (mod) {
                case 0:
                    throw new ArrayIndexOutOfBoundsException();
                case 1:
                    throw new IndexOutOfBoundsException();
                case 2:
                    throw new NumberFormatException();
                case 3:
                    throw new RuntimeException();
            }
            return null;
        }, context -> {
            logger.info("修复了");
            return null;
        });
    }



}
